use dta/SMDLevel.dta, clear

******************************************************************
******************************************************************
egen NoP1=rowtotal(run1_s-run1_nkp)
egen NoP2=rowtotal(run2_s-run2_nkp)

gen SECOND=0
replace SECOND=1 if votes2_TOTAL!=0  

gen NoPF=NoP1
replace NoPF=NoP2 if SECOND==1

foreach party in s v_a v a l t kir h_fv h fv oth nsa nkp {
gen runF_`party'=run1_`party'
replace runF_`party'=run2_`party' if SECOND==1
}

**************** ENOP FIRST ROUND *********************************

foreach party in s v_a v a l t kir h_fv h fv oth nsa nkp {
gen vs1SQ_`party'=vs1_`party'*vs1_`party'
}
egen temp=rowtotal(vs1SQ_s-vs1SQ_nkp)
gen ENoP1=1/temp
drop temp

**************** ENOP FINAL ROUND *********************************

foreach party in s v_a v a l t kir h_fv h fv oth nsa nkp {
gen vsF_`party'=vs1_`party'
replace vsF_`party'=vs2_`party' if SECOND==1
}

foreach party in s v_a v a l t kir h_fv h fv oth nsa nkp {
gen vsFSQ_`party'=vsF_`party'*vsF_`party'
}
egen temp=rowtotal(vsFSQ_s-vsFSQ_nkp)
gen ENoPF=1/temp
drop temp

********************************************************************
*********************** ANALYSIS ***********************************
********************************************************************

gen reform=0
replace reform=1 if year>1920

*** putting all minor parties into "oth"
*gen run1_OTH=run1_oth+run1_t+run1_kir
*gen runF_OTH=runF_oth+runF_t+runF_kir
*drop *_t *_kir *_oth

foreach r in 1 F {
gen RUN`r'FARM=run`r'_l
gen RUN`r'LIB=run`r'_v+run`r'_v_a+run`r'_a
gen RUN`r'LAB=run`r'_s+run`r'_nsa+run`r'_nkp
gen RUN`r'CON=run`r'_h+run`r'_h_fv+run`r'_fv
gen RUN`r'OTH=run1_oth+run1_t+run1_kir
}

*************************************************************************************
**********************REFORM EFFECT on ENOP FIRST / ENOP FINAL (CLUSTER SE) **********
*************************************************************************************

foreach r in 1 F {
eststo clear
xtreg ENoP`r' reform, i(SMD) fe cluster(PR)
eststo
estadd local DistrictFE "Yes"
estadd local Party "No"
xtreg ENoP`r' reform run`r'_l, i(SMD) fe cluster(PR)
eststo
estadd local DistrictFE "Yes"
estadd local Party "No"
xtreg ENoP`r' reform run`r'_l run`r'_nsa, i(SMD) fe cluster(PR)
eststo
estadd local DistrictFE "Yes"
estadd local Party "No"
xtreg ENoP`r' reform run`r'_l run`r'_nsa run`r'_nkp, i(SMD) fe cluster(PR)
eststo
estadd local DistrictFE "Yes"
estadd local Party "No"
xtreg ENoP`r' reform run`r'_*, i(SMD) fe cluster(PR)
eststo
estadd local DistrictFE "Yes"
estadd local Party "Yes"
esttab using tables/TableA2/Reform_ENoP`r'_cluster.tex, replace se b(%9.3f) se(%9.3f) r2 nostar nomtitles scalars("DistrictFE SMD fixed effects" "Party Party fixed effects") ///
drop(_cons run*s run*_v* run*_t run*_k* run*_h* run*_fv run*_a run*_oth) ///
coeflabels(_cons "Constant" reform "PR" run1_l "Farmers' party " runF_l "Farmers' party" run1_nsa "Soc. dem. labor party" runF_nsa "Soc. dem. labor party" run1_nkp "Communist party" runF_nkp "Communist party") ///
prehead("/begin{tabular}{l*{@M}{rr}}" "/hline") posthead(/hline) postfoot("/hline" "/end{tabular}")  
}


*************************************************************************************
**********************REFORM EFFECT on ENOP FIRST / ENOP FINAL (robust SE) **********
*************************************************************************************

foreach r in 1 F {
eststo clear
xtreg ENoP`r' reform, i(SMD) fe cluster(SMD)
eststo
estadd local DistrictFE "Yes"
estadd local Party "No"
xtreg ENoP`r' reform run`r'_l, i(SMD) fe cluster(SMD)
eststo
estadd local DistrictFE "Yes"
estadd local Party "No"
xtreg ENoP`r' reform run`r'_l run`r'_nsa, i(SMD) fe cluster(SMD)
eststo
estadd local DistrictFE "Yes"
estadd local Party "No"
xtreg ENoP`r' reform run`r'_l run`r'_nsa run`r'_nkp, i(SMD) fe cluster(SMD)
eststo
estadd local DistrictFE "Yes"
estadd local Party "No"
xtreg ENoP`r' reform run`r'_*, i(SMD) fe cluster(SMD)
eststo
estadd local DistrictFE "Yes"
estadd local Party "Yes"
esttab using tables/TableA2/Reform_ENoP`r'_clusterSMD.tex, replace se b(%9.3f) se(%9.3f) r2 nostar nomtitles scalars("DistrictFE SMD fixed effects" "Party Party fixed effects") ///
drop(_cons run*s run*_v* run*_t run*_k* run*_h* run*_fv run*_a run*_oth) ///
coeflabels(_cons "Constant" reform "PR" run1_l "Farmers' party " runF_l "Farmers' party" run1_nsa "Soc. dem. labor party" runF_nsa "Soc. dem. labor party" run1_nkp "Communist party" runF_nkp "Communist party") ///
prehead("/begin{tabular}{l*{@M}{rr}}" "/hline") posthead(/hline) postfoot("/hline" "/end{tabular}")  
}
